package com.lc.luzhengtang.adapter;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.activity.LoginActivity;
import com.lc.luzhengtang.activity.ShopDetailActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.conn.Conn;
import com.lc.luzhengtang.conn.ConnAddShop;
import com.lc.luzhengtang.conn.ConnCommodity;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * 文 件 名: NewsListAdapter
 * 创 建 人: zhangyu
 * 创建日期: 16/12/24 19:55
 * 邮   箱: yuanchong121@gmail.com
 * 修改时间：
 * 修改备注：
 */
public class ClassfiyGridAdapter extends BaseQuickAdapter<ConnCommodity.DataBeanX.DataBean, BaseViewHolder> {
    View view_lines;
    List<ConnCommodity.DataBeanX.DataBean> data;
ImageView img_add_shop;
    public ClassfiyGridAdapter(List<ConnCommodity.DataBeanX.DataBean> data) {
        super(R.layout.item_classfiy_detail, data);
        this.data = data;
    }


    @Override
    protected void convert(BaseViewHolder helper, final ConnCommodity.DataBeanX.DataBean item) {
        ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) helper.convertView);
        view_lines = helper.getView(R.id.view_lines);
//
        int position = helper.getPosition();

//        if (data.size() > 1) {
        if (position % 2 == 1) {
            ScaleScreenHelperFactory.getInstance().loadViewMargin(helper.convertView.findViewById(R.id.ll_content), 0, 23, 30, 0);
        } else {
            ScaleScreenHelperFactory.getInstance().loadViewMargin(helper.convertView.findViewById(R.id.ll_content), 0, 23, 24, 0);
        }

        helper.setText(R.id.text_nicheng, item.getTitle());
        helper.setText(R.id.text_money, "￥" + item.getSales());
        helper.setText(R.id.text_xiaoliang, "销量：" + item.getVolume());

        Glide.with(mContext).load(Conn.HOST2 + item.getPicurl()).crossFade().error(R.mipmap.zhanwei).placeholder(R.mipmap.zhanwei).into((ImageView) helper.getView(R.id.img_shouyi));
        helper.addOnClickListener(R.id.img_add_shop);

        final View view = helper.getConvertView();

        img_add_shop=helper.getView(R.id.img_add_shop);


        img_add_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(BaseApplication.basePreferences.readUid())){
                    mContext.startActivity(new Intent(mContext, LoginActivity.class));
                }else {

                    new ConnAddShop(item.getId() + "", BaseApplication.basePreferences.readUid(), "1", "盒", item.getSales(), new AsyCallBack<CommenBean>() {

                        @Override
                        public void onStart(int type) throws Exception {
                            super.onStart(type);
                        }

                        @Override
                        public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                            super.onSuccess(toast, type, o, commenBean);
                            //  Toast.makeText(getActivity(), "成功加入购物车", Toast.LENGTH_SHORT).show();
                            LovelyToast.makeText(mContext, "成功加入购物车", 0, LovelyToast
                                    .SUCCESS, LovelyToast.SCALE);
                        }

                        @Override
                        public void onFail(String toast, int type) throws Exception {
                            super.onFail(toast, type);
                        }

                        @Override
                        public void onEnd(String toast, int type) throws Exception {
                            super.onEnd(toast, type);
                            //可能需要移除之前添加的布局

                        }
                    }

                    ).execute(mContext, false);
                }
            }
        });



        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("goods_id", item.getId() + "");
                intent.putExtra("img_url", item.getPicurl());
                intent.setClass(mContext, ShopDetailActivity.class);
                mContext.startActivity(intent);
            }
        });

        //让线消失
     /*   view_lines = helper.getView(R.id.view_lines);
        if (data.size() % 2 == 0) {
            helper.setVisible(R.id.view_lines, false);
        } else {
            helper.setVisible(R.id.view_lines, true);
        }
*/

    }


}
